import { defineStore } from "pinia"; //引入pinia
import * as Api from "@/api/index";
import { ElMessage } from "element-plus";
const useStore = defineStore("about", {
  state: () => {
    return {
      carList: [],
      list: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        layout: "total,pager,prev, next",
      }, //为了避免出错，返回的值用()包起来
    };
  },
  actions: {
    changeList() {
      Api.getCity().then((res) => {
        this.carList = res.list.slice(0, 10);
        this.list = res.list;
        this.page.total = res.list.length;
      });
    },
    changeData(obj) {
      this.carList = this.list.filter((item) => {
        return item.city.includes(obj.city);
      });
    },
    pageChange(page) {
      this.page.total = this.list.length;
      //模拟分页
      if (page.currentPage === 1) {
        this.carList = this.list.slice(
          (page.currentPage - 1) * 10,
          page.currentPage * 10
        );
      } else if (page.currentPage == 2) {
        this.carList = this.list.slice(
          (page.currentPage - 1) * 10,
          page.currentPage * 10
        );
      }
    },
    addCarList(val) {
      this.carList.unshift({
        id: 0,
        city: val.inp,
        state: 0,
      });
      ElMessage({
        message: "新增车站成功",
        type: "success",
      });
    },
    addTab(tableData) {
      this.carList = this.carList.concat(tableData);
      this.page.total = this.carList.length;
    },
  },
});
export { useStore };
